# cmrc2018_train.json
# cmrc2018_dev.json
# cmrc2018_trial.json
# .json -> mindrecord
# export Document=["data"][i]["paragraphs"][j]["context"]
# export Question=["data"][i]["paragraphs"][j]["qas"][k]["question"]
# export   Answer=["data"][i]["paragraphs"][j]["qas"][k]["answers"][0]["text"]
#  -> "阅读文章：$Document\n问：$Question\n答：...\n$Answer"
import os, sys
import json

template = "阅读文章：{}\n问：{}\n答：...\n{}\n\n"
def read_json(file):
    assert os.path.isfile(file)
    with open(file, "r", encoding='UTF-8') as f:
        dic = json.load(f)
    return dic
def read_txt(file):
    assert os.path.isfile(file)
    with open(file, 'r', encoding='utf-8') as f:
        stri = f.read()
    return stri

def entry2record(entry, tamplate=template):
    template_string = ''
    for paragraph in entry["paragraphs"]:
        Document = paragraph["context"]
        for qa in paragraph["qas"]:
            Question = qa["question"]
            Answer = ''
            if "answers" in qa.keys():
                Answer = qa["answers"][0]["text"]
            template_string += tamplate.format(Document, Question, Answer)
    return template_string

def entry_iterator(file_path):
    input_data = read_json(file_path)['data']
    print(file_path, ': data len', len(input_data))
    return iter(input_data)

def sample_app(trial_file, temp_file):
    temp_txt = read_txt(temp_file)
    for entry in entry_iterator(trial_file):
        template_string = entry2record(entry)
        if template_string==temp_txt:
            print("transforming correct")
            return template_string==temp_txt
    return False

if __name__ == "__main__":
    # use a sample in cmrc2018_trial.json
    # to ensure the transforming correct
    # comparing with temp/sampledata2.txt
    train_file = "squad-style-data/cmrc2018_train.json"
    dev_file   = "squad-style-data/cmrc2018_dev.json"
    trial_file = "squad-style-data/cmrc2018_trial.json"
    is_template_ok = sample_app(
        trial_file=trial_file, temp_file="temp/sampledata2.txt"
    )
    print("is_template_ok", is_template_ok)
    pass